Data sharing between multiple instances of a class

  • Note

    Using Static Properties:

    Define a static property within the class to store shared data. This way, the data is associated with the class itself, and any instance of the class can access and modify it.

    
                     class Example {
                        private static $sharedData;
    
                        public function setData($data) {
                            self::$sharedData = $data;
                        }
    
                        public function getData() {
                            return self::$sharedData;
                        }
                    }
    
                    // Example usage with multiple instances
                    $instance1 = new Example();
                    $instance2 = new Example();
    
                    $instance1->setData("Hello, World!");
                    echo $instance2->getData();  // Outputs "Hello, World!"